//
//  LZStatusCell.swift
//  Weibo
//
//  Created by lzing on 16/3/25.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SnapKit

class LZStatusCell: UITableViewCell {
    
    var bottomViewBottomConstraint : Constraint?
    
    //MARK: - property
    ///  模型属性
    var status : LZStatus? {
        didSet {
            originView.status = status
            // 卸载约束
            bottomViewBottomConstraint?.uninstall()
            // 判断是否有转发微博
            if let retweetStatus = status?.retweeted_status {
                // 显示转发的view
                retweetView.hidden = false
                // 转发模型数据
                retweetView.retweetStatus = retweetStatus
                // 添加约束
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                   self.bottomViewBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                // 隐藏转发的view
                retweetView.hidden = true
                // 添加约束
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        // 添加子控件
        addSubviews()
        // 添加约束
        snapSubview()
        // 优化cell
        optimizeCell()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - addSubView
    private func addSubviews() {
        contentView.addSubview(originView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
    }
    
    //MARK: - snapSubview
    private func snapSubview() {
        // 添加约束
        originView.snp_makeConstraints { (make) -> Void in
            // 左边,右边等于contentView的左边,右边
            make.right.left.equalTo(contentView)
            // 上边等于contentView偏移10
            make.top.equalTo(contentView).offset(10)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            // 左边,右边等于contentView
            make.right.left.equalTo(contentView)
            // 上边等于originView的底部
            self.bottomViewBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            // 高度
            make.height.equalTo(44)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            // 上边,左边,右边等于cell的上边,左边,右边
            make.top.left.right.equalTo(self)
            // 底部等于原始view的底部
            make.bottom.equalTo(bottomView)
        }
    }
    
    //MARK: - optimizeCell
    private func optimizeCell() {
        // 异步绘制
        self.layer.drawsAsynchronously = true
        // 栅格化/光栅化
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
    
    //MARK: - lazy
    /// 原始view
    private lazy var originView : LZStatusOriginView = LZStatusOriginView()
    
    /// 底部view
    private lazy var bottomView : LZStatusBottomView = LZStatusBottomView()
    
    ///  转发微博
    private lazy var retweetView : LZStatusRetweetView = LZStatusRetweetView()
}
